﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using ICeFramework.DataAccess.Interfaces;
using ICeFramework.DataAccess.Enums;
using System.Data;
using ICeFramework.Common.DataLayer.Enums;

namespace ICeFramework.DataAccess.SqlConnections
{
    public class MSSqlServerConnection : ISqlConnection
    {
        private string _connectionString;
        private string _serverName;
        private string _userName;
        private string _password;
        private string _database;

        private SqlConnection _connection;
        #region public SqlConnection Connection
        /// <summary>
        /// Gets the Connection of the MSSqlServerConnection
        /// </summary>
        /// <value></value>
        public SqlConnection Connection
        {
            get { return _connection; }
        }
        #endregion

        #region public MSSqlServerConnection(string connectionString, string serverName, string userName, string password)
        /// <summary>
        /// Initializes a new instance of the <b>MSSqlServerConnection</b> class.
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="serverName"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        public MSSqlServerConnection(string connectionString, string serverName, string userName, string password)
        {
            _connection = new SqlConnection(connectionString);
            _connection.Open();
            _connectionString = connectionString;
            _serverName = serverName;
            _userName = userName;
            _password = password;
            _database = _connection.Database;
        }
        #endregion

        #region public MSSqlServerConnection(string connectionString)
        /// <summary>
        /// Initializes a new instance of the <b>MSSqlServerConnection</b> class.
        /// </summary>
        /// <param name="connectionString"></param>
        public MSSqlServerConnection(string connectionString)
        {
            _connection = new SqlConnection(connectionString);
            //_connection.Open();
            _serverName = _connection.DataSource;
            _userName = string.Empty;
            _password = string.Empty;
            _database = _connection.Database;
        }
        #endregion

        #region public string ConnectionString
        /// <summary>
        /// Gets the ConnectionString of the MSSqlServerConnection
        /// </summary>
        /// <value></value>
        public string ConnectionString
        {
            get { return _connectionString; }
        }
        #endregion

        #region public bool Connected
        /// <summary>
        /// Gets the Connected of the MSSqlServerConnection
        /// </summary>
        /// <value></value>
        public bool Connected
        {
            get { return _connection.State != ConnectionState.Closed; }
        }
        #endregion

        #region public ConnectionState State
        /// <summary>
        /// Gets the State of the MSSqlServerConnection
        /// </summary>
        /// <value></value>
        public ConnectionState State
        {
            get
            {
                return _connection.State;
            }
        }
        #endregion

        #region public void Close()
        /// <summary>
        /// 
        /// </summary>
        public void Close()
        {
            if (_connection != null && _connection.State != ConnectionState.Closed)
            {
                _connection.Close();
                _connection.Dispose();
            }
        }
        #endregion

        #region public ServerType ConnectionType
        /// <summary>
        /// Gets the ConnectionType of the MSSqlServerConnection
        /// </summary>
        /// <value></value>
        public ServerType ConnectionType
        {
            get { return ServerType.MsSql; }
        }
        #endregion

        #region public string ServerName
        /// <summary>
        /// Get/Sets the ServerName of the MSSqlServerConnection
        /// </summary>
        /// <value></value>
        public string ServerName
        {
            get
            {
                return _serverName;
            }
            set
            {
                _serverName = value;
            }
        }
        #endregion

        #region public string UserName
        /// <summary>
        /// Get/Sets the UserName of the MSSqlServerConnection
        /// </summary>
        /// <value></value>
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
            }
        }
        #endregion

        #region public string Password
        /// <summary>
        /// Get/Sets the Password of the MSSqlServerConnection
        /// </summary>
        /// <value></value>
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
            }
        }
        #endregion

        #region public string Database
        /// <summary>
        /// Get/Sets the Database of the MSSqlServerConnection
        /// </summary>
        /// <value></value>
        public string Database
        {
            get
            {
                return _database;
            }
            set
            {
                _database = value;
            }
        }
        #endregion
    }
}

